package cn.jekun.shop.app.action;

import javax.annotation.Resource;

import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.shop.common.service.AddressService;

/**
 * 收货地址
 * @author twj
 *
 */
@SuppressWarnings("serial")
public class AddressAction extends AppBaseAction{
	@Resource(name = AddressService.BEAN_NAME)
	protected AddressService addressService;
	/**
	 * 删除收货地址
	 * @return
	 */
	public String delAddress(){
		JSONObject jsonObject = null;
		try{
			String addressId = this.request.getParameter("addressId");
			this.validateLoginApp();
			this.validateSign(getLoginSign(),getUserId(),addressId);
			jsonObject = addressService.del(addressId);
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "删除收货地址成功，来源:mobile");//记录操作日志

		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "删除收货地址失败，原因:"+jsonObject.get("message")+",来源:mobile");//记录操作日志

		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
			return this.writeJson(jsonObject);
		}
	/**
	 * 保存收货地址
	 * @return
	 */
	public String saveAddress(){
		JSONObject jsonObject = null;
		try{
			String userId = this.request.getParameter("userId");
			String provinceId = this.request.getParameter("provinceId");
			String cityId = this.request.getParameter("cityId");
			String regionId = this.request.getParameter("regionId");
			String street = this.request.getParameter("street");
			String shipName = this.request.getParameter("shipName");
			String shipZip = this.request.getParameter("shipZip");
			String shipTel = this.request.getParameter("shipTel");
			String mobile = this.request.getParameter("shipMobile");
			String def = this.request.getParameter("def");
			String memo = this.request.getParameter("memo");
			String lng = this.request.getParameter("lng");//经度
			String lat = this.request.getParameter("lat");//纬度
			this.validateLoginApp();
			this.validateSign(getLoginSign(),getUserId(),provinceId,cityId,regionId,shipZip,shipTel,mobile,def,lng,lat);
			jsonObject = addressService.save(userId, provinceId, cityId, regionId, street, shipName, shipZip, shipTel, mobile, def, memo,lng,lat);
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "保存收货地址成功，来源:mobile");//记录操作日志

		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "保存收货地址失败，原因:"+jsonObject.get("message")+",来源:mobile");//记录操作日志

		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
			return this.writeJson(jsonObject);
		}
	/**
	 * 收货地址列表
	 * @return
	 */
	public String addressList(){
		JSONObject jsonObject = null;
		try{
			String pageSize = this.request.getParameter("pageSize");
			String currentPage = this.request.getParameter("currentPage");
			String userId = this.request.getParameter("userId");
			this.validateLoginApp();
			this.validateSign(getLoginSign(),getUserId(),pageSize,currentPage);
			jsonObject = addressService.query(pageSize, currentPage,userId);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
			return this.writeJson(jsonObject);
		}
	/**
	 * 加载收货地址信息
	 * @return
	 */
	public String loadAddress(){
		JSONObject jsonObject = null;
		try{
			String addressId = this.request.getParameter("addressId");
			this.validateLoginApp();
			this.validateSign(getLoginSign(),getUserId(),addressId);
			jsonObject = addressService.load(addressId);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
			return this.writeJson(jsonObject);
		}
	/**
	 * 修改收货地址信息
	 * @return
	 */
	public String updateAddress(){
		JSONObject jsonObject = null;
		try{
			String addressId = this.request.getParameter("addressId");
			String userId = this.request.getParameter("userId");
			String provinceId = this.request.getParameter("provinceId");
			String cityId = this.request.getParameter("cityId");
			String regionId = this.request.getParameter("regionId");
			String street = this.request.getParameter("street");
			String shipName = this.request.getParameter("shipName");
			String shipZip = this.request.getParameter("shipZip");
			String shipTel = this.request.getParameter("shipTel");
			String mobile = this.request.getParameter("shipMobile");
			String def = this.request.getParameter("def");
			String memo = this.request.getParameter("memo");
			String flag = this.request.getParameter("flag");
			String lng = this.request.getParameter("lng");
			String lat = this.request.getParameter("lat");
			this.validateLoginApp();
			this.validateSign(getLoginSign(),getUserId(),addressId,provinceId,cityId,regionId,shipZip,shipTel,mobile,def,flag,lng,lat);
			jsonObject = addressService.update(addressId, userId, provinceId, cityId, regionId, street, shipName, shipZip, shipTel, mobile, def, memo,flag,lng,lat);
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "修改收货地址成功，来源:mobile");//记录操作日志

		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "修改收货地址失败，原因:"+jsonObject.get("message")+",来源:mobile");//记录操作日志

		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
			return this.writeJson(jsonObject);
		}

	
	/**
	 * 设置为默认地址
	 * @return
	 */
	public String updateDefaultAddress(){
		JSONObject jsonObject = new JSONObject();
		try{
			String addressId = this.request.getParameter("addressId");
			this.validateLoginApp();
			this.validateSign(getLoginSign(),getUserId(),addressId);
			this.addressService.updateDefault(getUserId(), addressId);
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			jsonObject.put(ErrorMsg.KEY_MESSAGE, "调用成功");	
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
			return this.writeJson(jsonObject);
	}

}
